package com.csea.common.converter;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.csea.common.domain.dos.ConfusingWordAnalysis;
import com.csea.common.domain.dos.PublicIdiomDo;

import java.util.ArrayList;
import java.util.List;

public class PublicIdiomConverter {
    public static PublicIdiomDo convert(String json) {
        JSONObject root = JSON.parseObject(json);
        Integer classificationId = root.getInteger("classificationId");
        JSONObject data = root.getJSONObject("data");

        if (classificationId == null || classificationId != 1 || data == null) {
           return null;
        }

        PublicIdiomDo publicIdiomDo = JSON.parseObject(data.toJSONString(), PublicIdiomDo.class);

        publicIdiomDo.setPopularity(0);
        publicIdiomDo.setEmotionalColor(data.getString("emotional_color"));

        // 混淆词分析字段映射
        JSONArray analysisArray = data.getJSONArray("confusing_word_analysis");
        List<ConfusingWordAnalysis> analysisList = (analysisArray != null)
                ? analysisArray.toJavaList(ConfusingWordAnalysis.class)
                : new ArrayList<>();
        publicIdiomDo.setConfusingWordAnalysis(analysisList);

        return publicIdiomDo;
    }
}